package com.huabo.system.service.impl;

import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.PageInfo;
import com.huabo.system.config.DateBaseConfig;
import com.huabo.system.mysql.entity.TblFlowInnerRuleMySql;
import com.huabo.system.mysql.entity.TblInnerruleMySql;
import com.huabo.system.mysql.entity.TblOuterruleMySql;
import com.huabo.system.mysql.mapper.TblFlowInnerRuleMySqlMapper;
import com.huabo.system.mysql.mapper.TblInnerruleMySqlMapper;
import com.huabo.system.mysql.mapper.TblOuterruleMySqlMapper;
import com.huabo.system.oracle.entity.TblFlowInnerRule;
import com.huabo.system.oracle.entity.TblInnerrule;
import com.huabo.system.oracle.entity.TblOuterrule;
import com.huabo.system.oracle.mapper.TblFlowInnerRuleMapper;
import com.huabo.system.oracle.mapper.TblInnerruleMapper;
import com.huabo.system.oracle.mapper.TblOuterruleMapper;
import com.huabo.system.service.TblFlowInnerRuleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TblFlowInnerRuleServiceImpl implements TblFlowInnerRuleService {

    @Resource
    private TblFlowInnerRuleMapper tblFlowInnerRuleMapper;
    @Resource
    private TblInnerruleMapper tblInnerruleMapper;
    @Resource
    private TblOuterruleMapper tblOuterruleMapper;


    @Resource
    private TblFlowInnerRuleMySqlMapper tblFlowInnerRuleMySqlMapper;
    @Resource
    private TblInnerruleMySqlMapper tblInnerruleMySqlMapper;
    @Resource
    private TblOuterruleMySqlMapper tblOuterruleMySqlMapper;


    @Override
    public void saveTblFlowInnerRule(TblFlowInnerRule inner) {
        tblFlowInnerRuleMapper.insertRule(inner);
    }

    @Override
    public void saveMySqlTblFlowInnerRule(TblFlowInnerRuleMySql innerRuleMySql) {
        tblFlowInnerRuleMySqlMapper.insertRule(innerRuleMySql);
    }

    @Override
    public Map<String, Object> findByOrgidAndFlowidobj(String flowid, Integer pageNumber, Integer pageSize, String token) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            TblStaffUtil user = DealUserToken.parseUserToken(token);
            if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
                PageInfo<TblInnerrule> pageInfo = new PageInfo<TblInnerrule>();
                pageInfo.setPageSize(pageSize);
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setTlist(tblInnerruleMapper.findInnerruleByFolwid(pageInfo, user.getCurrentOrg().getOrgid().toString(), flowid));
                pageInfo.setTotalRecord(tblInnerruleMapper.findCountInnerruleByFolwid(user.getCurrentOrg().getOrgid().toString(), flowid));
                resultMap.put("data", pageInfo);
            } else {

                PageInfo<TblInnerruleMySql> pageInfo = new PageInfo<TblInnerruleMySql>();
                pageInfo.setPageSize(pageSize);
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setTlist(tblInnerruleMySqlMapper.findInnerruleByFolwid(pageInfo, user.getCurrentOrg().getOrgid().toString(), flowid));
                pageInfo.setTotalRecord(tblInnerruleMySqlMapper.findCountInnerruleByFolwid(user.getCurrentOrg().getOrgid().toString(), flowid));
                resultMap.put("data", pageInfo);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public List<TblFlowInnerRule> isIfFlowInner(String flowid, String[] innerid) {
        List<TblFlowInnerRule> list = new ArrayList<>();
        for (String i : innerid) {
            TblFlowInnerRule innerRule = this.tblFlowInnerRuleMapper.selectFlowInner(flowid, i);
            list.add(innerRule);
        }
        return list;
    }

    @Override
    public List<TblFlowInnerRuleMySql> isIfMySqlFlowInner(String flowid, String[] innerid) {
        List<TblFlowInnerRuleMySql> list = new ArrayList<>();
        for (String i : innerid) {
            TblFlowInnerRuleMySql innerRule = this.tblFlowInnerRuleMySqlMapper.selectFlowInner(flowid, i);
            list.add(innerRule);
        }
        return list;
    }

    @Override
    public void deleteTblFlowInnerRule(TblFlowInnerRule inner) {
        this.tblFlowInnerRuleMapper.delteTblFlowInnerRule(inner);
    }

    @Override
    public void deleteMySqlTblFlowInnerRule(TblFlowInnerRuleMySql innerRuleMySql) {
        this.tblFlowInnerRuleMySqlMapper.delteTblFlowInnerRule(innerRuleMySql);
    }

    @Override
    public Map<String, Object> findOutRuleByOrgidAndFlowid(String name, String status, String flowid, Integer pageNumber, Integer pageSize, String token) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        try {
            TblStaffUtil user = DealUserToken.parseUserToken(token);
            if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
                PageInfo<TblOuterrule> pageInfo = new PageInfo<TblOuterrule>();
                pageInfo.setPageSize(pageSize);
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setTlist(tblOuterruleMapper.findInnerruleByFolwid(pageInfo, user.getCurrentOrg().getOrgid().toString(), name, status, flowid));
                pageInfo.setTotalRecord(tblOuterruleMapper.findCountInnerruleByFolwid(user.getCurrentOrg().getOrgid().toString(), name, status, flowid));

                resultMap.put("data", pageInfo);
            } else {
                PageInfo<TblOuterruleMySql> pageInfo = new PageInfo<TblOuterruleMySql>();
                pageInfo.setPageSize(pageSize);
                pageInfo.setCurrentPage(pageNumber);
                pageInfo.setTlist(tblOuterruleMySqlMapper.findInnerruleByFolwid(pageInfo, user.getCurrentOrg().getOrgid().toString(), name, status, flowid));
                pageInfo.setTotalRecord(tblOuterruleMySqlMapper.findCountInnerruleByFolwid(user.getCurrentOrg().getOrgid().toString(), name, status, flowid));

                resultMap.put("data", pageInfo);
            }

            resultMap.put("code", "1");
            resultMap.put("msg", "数据访问成功");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public void delteTblFlowInnerRule(TblFlowInnerRule inner) {
    	if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
    		this.tblFlowInnerRuleMapper.delteTblFlowInnerRule(inner);	
        }else {
        	this.tblFlowInnerRuleMapper.delteTblFlowInnerRule(inner);
		}
    	
    	
    	
    }
}
